import { _decorator, animation, Animation, Collider2D, Component, Contact2DType, instantiate, IPhysics2DContact, Node, PhysicsSystem2D, Prefab, Sprite } from 'cc';
const { ccclass, property } = _decorator;

const state = {
    stand: 0,
    attack: 1,
    hurt: 2
}

@ccclass('enemy')
export class enemy extends Component {
    @property(Node)
    hero: Node
    @property(Node)
    enemyHp: Node

    hp = 5;
    //受伤判定
    isHit = false;

    animation = 'idle'
    enemyState = state.stand
    speed = 60
    //判断攻击
    enemyattcak = 1
    //判定死亡
    enemylife = false
    //
    isaction: boolean = false

    start() {
        let animation = this.node.getComponent(Animation);
        animation.on(Animation.EventType.FINISHED, this.onAnimationEvent, this)
    }

    //动画回调
    onAnimationEvent(e, data) {
        if (data.name == 'hurt') {
            this.isaction = false
            this.hp--;
            this.lifebar(this.hp)
            console.log(this.hp)
            if (this.hp <= 0) {
                this.enemylife = true
                this.setAnima('dead');
            } else {
                this.enemyattcak = 1
                this.setAnima('attack');
            }
        } else if (data.name == 'attack') {
            this.animation = 'run'
            this.setAnima('attack');
        } else if (data.name == 'dead') {
            this.node.destroy();
        }
    }
    //血条
    lifebar(hp) {
        const sprite = this.enemyHp.getComponent(Sprite)
        sprite.fillRange = hp / 5
    }
    lifebarScaleX(index) {
        if (index > 0)
            this.enemyHp.setScale(1 * this.node.getScale().x, this.enemyHp.getScale().y);
        else
            this.enemyHp.setScale(1 * this.node.getScale().x, this.enemyHp.getScale().y);
    }

    attack() {
        if (this.isHit) {
            this.enemyattcak = 0
            this.setAnima('hurt')
            this.isHit = false
            this.isaction = true
        }
        if (this.enemyattcak) {
            this.setAnima('attack');
        }
    }

    //移动
    initMove(deltaTime) {
        let anim = this.animation
        let index = 0
        let getx = this.node.worldPosition.x - this.hero.worldPosition.x
        if (!this.isaction) {
            if (this.enemyState) {
                if (getx >= 20) {
                    index = -1
                } else if (getx < -20) {
                    index = 1
                } else {
                    index = 0
                    this.attack()
                }
                if (index) {
                    const x = this.node.position.x + index * this.speed * deltaTime;
                    //反转角色
                    let scaleX = Math.abs(this.node.getScale().x);
                    this.node.setScale(index * scaleX, this.node.getScale().y);
                    this.lifebarScaleX(index)
                    //改变位置
                    this.node.setPosition(x, this.node.position.y);
                    //调用动画方法
                    anim = "walk"
                    this.setAnima(anim)
                }
            } else {
                let x = Math.abs(getx)
                if (x <= 150) {
                    this.enemyState = state.attack
                }
            }
        }
    }

    setAnima(anima) {
        if (this.animation == anima) return;
        const animationComponent = this.node.getComponent(Animation);
        this.animation = anima;
        animationComponent.play(anima);
    }

    update(deltaTime: number) {
        if (!this.enemylife) {
            this.initMove(deltaTime)
        }
    }
}


